package com.keeson.android_cli.presenter;

import android.content.Context;
import com.google.gson.Gson;
import com.keeson.android_cli.App;
import com.keeson.android_cli.base.BasePresenterImpl;
import com.keeson.android_cli.base.BaseRespose;
import com.keeson.android_cli.interfaces.contract.HelpContract;
import com.keeson.android_cli.util.JsonHelp;
import com.keeson.android_cli.util.LogUtils;
import com.keeson.android_cli.util.cache.UserDataCache;
import com.keeson.android_cli.util.luban.Luban;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by jiaqiZHou
 * on 2020/12/4
 */
public class HelpPresenter extends BasePresenterImpl<HelpContract.View> implements HelpContract.Presenter {

    public HelpPresenter(HelpContract.View view) {
        super(view);
    }

    @Override
    public void sendMultipart(ArrayList<File> path, Context context, String id, int type, String feedback_text, String phone) {
        view.showProgress(null);
        try {
            List<File> files1 = Luban.with(App.getApp()).load(path).get();
            String url = App.getBaseUrl() + "/qs-gasbag-system/api/v1/user/addFeedbackInfo";
            sendMultipart(url,context, path,id,type,feedback_text,phone);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 上传文件及参数
     */
    private void sendMultipart(String url, Context context, List<File> list,String id, int type, String feedback_text, String phone) {
        File sdcache = context.getExternalCacheDir();
        int cacheSize = 10 * 1024 * 1024;
        //设置超时时间及缓存
        OkHttpClient.Builder builder = null;
        if (sdcache != null) {
            builder = new OkHttpClient.Builder()
                    .connectTimeout(15, TimeUnit.SECONDS)
                    .writeTimeout(20, TimeUnit.SECONDS)
                    .readTimeout(20, TimeUnit.SECONDS)
                    .cache(new Cache(sdcache.getAbsoluteFile(), cacheSize));
        }
        OkHttpClient mOkHttpClient = builder.build();
        MultipartBody.Builder mbody = new MultipartBody.Builder().setType(MultipartBody.FORM);
        List<File> fileList = list;
        int i = 0;
        for (File file : fileList) {
            if (file.exists()) {
                mbody.addFormDataPart("files", file.getName(), RequestBody.create(MediaType.parse("image/png"), file));
                i++;
            }
        }
        mbody.addFormDataPart("id",id);
        mbody.addFormDataPart("type",type+"");
        mbody.addFormDataPart("feedback_text",feedback_text+"");
        mbody.addFormDataPart("phone",phone+"");

        RequestBody requestBody = mbody.build();
        Request request = new Request.Builder()
                .header("Authorization", UserDataCache.getInstance().getToken())
                .url(url)
                .post(requestBody)
                .build();

        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                view.dismissProgress();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                view.dismissProgress();
                if (response.body() != null) {
                    String res = response.body().string();
                    LogUtils.e("上传文件接口=====", res +"");
                    if(JsonHelp.isGoodJson(res)){
                        BaseRespose<String> uploadResult = new Gson().fromJson(res, BaseRespose.class);
                        if(uploadResult.isSuccess()){
                            view.sendMultipartSuccess(uploadResult.getData());
                        }else {

                        }
                    }


                }
            }
        });
    }


}